package main

import (
	"context"
	"encoding/json"
	"fmt"
	"os"

	"github.com/apache/rocketmq-client-go/v2"
	"github.com/apache/rocketmq-client-go/v2/primitive"
	"github.com/apache/rocketmq-client-go/v2/producer"
)

type PtsExtend struct {
	CompanyID     string `json:"company_id"`
	DepID         string `json:"dep_id"`
	Summary       string `json:"summary"`
	LicenseType   string `json:"licenseType"`
	LicenseNumber string `json:"licenseNumber"`
}

type Record struct {
	CustomerID  string `json:"customerId"`
	ConsumeID   string `json:"consumeId"`
	ServiceCode string `json:"serviceCode"`
	ServiceType string `json:"ServiceType"`
	Repeat      bool   `json:"repeat"`
	ConsumeTime string `json:"consumeTime"`
	RequestID   string `json:"requestId"`
	PtsExtend `json:"ptsExtend"`
}


func main() {

	namespace := "test"
	topicName := "oec-consume-record"

	record := Record{
		CustomerID:  "123123",
		ConsumeID:   "124234",
		ServiceCode: "pts_bn",
		ServiceType: "01",
		Repeat:      false,
		ConsumeTime: "2020-12-7 18:55:11",
		RequestID:   "45454564545",
		PtsExtend:   PtsExtend{},
	}

	data, _ := json.Marshal(record)

	p, err := rocketmq.NewProducer(
		producer.WithNsResovler(primitive.NewPassthroughResolver([]string{"10.199.150.168:9876"})),
		producer.WithRetry(2),
		//producer.WithCredentials(primitive.Credentials{
		//	AccessKey: "RocketMQ",
		//	SecretKey: "12345678",
		//}),
		producer.WithNamespace(namespace),
	)

	if err != nil {
		fmt.Println("init producer error: " + err.Error())
		os.Exit(0)
	}

	err = p.Start()
	if err != nil {
		fmt.Printf("start producer error: %s", err.Error())
		os.Exit(1)
	}

	for i := 0; i < 1000; i++ {
		res, err := p.SendSync(context.Background(), primitive.NewMessage(topicName, data))

		if err != nil {
			fmt.Printf("send message error: %s\n", err)
		} else {
			fmt.Printf("send message success: result=%s\n", res.String())
		}
	}

	err = p.Shutdown()
	if err != nil {
		fmt.Printf("shutdown producer error: %s", err.Error())
	}

	fmt.Println("record: ", record)
}